/**
 * 
 */

function dFileSW(file) {
	$("#dialogsecretform #downfile").val(file);
	$("#dialogsecretform #downfilename").text(file);
	$("#dialogsecretform").dialog("open");
	
	return false;
}

function lockForm(file, mode) {
	$("#dialogsecretform3 #lockfile" + mode).val(file);
	$("#dialogsecretform3 #lockfilename" + mode).text(file);
	$("#dialogsecretform3 form").hide();
	$("#dialogsecretform3 #frm" + mode).show();
	if (mode == 1) $("[aria-labelledby=ui-dialog-title-dialogsecretform3] .ui-dialog-buttonset button:first span").text("Protect");
	else if (mode == 2) $("[aria-labelledby=ui-dialog-title-dialogsecretform3] .ui-dialog-buttonset button:first span").text("Unprotect");
	$("#dialogsecretform3").dialog("open");
	
	return false;
}

//run the currently selected effect
function runEffect(elem) {
	
	// get effect type from 
	var selectedEffect = "blind";
	
	// most effect types need no options passed by default
	var options = {};
	
	// run the effect
	$(elem).hide( selectedEffect, options, 2000 );
};

function waitAndEffekt(elem) {
	window.setTimeout("runEffect('"+elem+"')", 5000);
}

function checkForm(formElem) {
	if (formChecked == true) return true;
	
	if ($(formElem + " #username").length > 0 && $(formElem + " #username").val().length <= 0) {
		$(formElem + " #username").addClass("ui-state-error");
		$(formElem + " .validateTips").text("The username must not be empty!");
		formChecked = false;
		return formChecked;
	}
	else if ($(formElem + " #secretword").val().length <= 0) {
		$(formElem + " #secretword").addClass("ui-state-error");
		$(formElem + " .validateTips").text("The secret phrase must not be empty!");
		formChecked = false;
		return formChecked;
	}
	else {
		// verschluesseln
		var enc = "";
		enc = $.sha256($(formElem + " #secretword"));
		$(formElem + " #secword").val(enc);
		
		if ($(formElem + " #secword").val().length <= 0) {
			$(formElem + " .validateTips").text("There is an error with encrypting the secret phrase!");
			$(formElem + " #secretword").removeClass("ui-state-error");
			formChecked = false;
			return formChecked;
		}
	}
	
	$(formElem).dialog("close");
	formChecked = true;
	return formChecked;
}

function checkLockForm() {
	if (formChecked == true) return true;
	
	if ($("#dialogsecretform3 #frm1").is(':visible')) {
		// Passwort vergeben
		var frm = $("#dialogsecretform3 #frm1");
		if ($("#secretword", frm).val().length <= 0) {
			$("#secretword", frm).addClass("ui-state-error");
			$(".validateTips", frm).text("The secret phrase must not be empty!");
			formChecked = false;
			return formChecked;
		}
		else {
			// verschluesseln
			var enc = "";
			enc = $.sha256($("#secretword", frm));
			$("#secword", frm).val(enc);
			
			if ($("#secword", frm).val().length <= 0) {
				$(".validateTips", frm).text("There is an error with encrypting the secret phrase!");
				$("#secretword", frm).removeClass("ui-state-error");
				formChecked = false;
				return formChecked;
			}
		}
	}
	else {
		// Passwort entfernen
		$("#dialogsecretform3 #secword").val("unlock");
	}
	
	$("#dialogsecretform3").dialog("close");
	formChecked = true;
	return formChecked;
}

$(document).ready(function(){
	
	$("#jsHint").hide();
	$(".line_even, .line_odd").not('.desc').css({'cursor': 'hand', 'cursor': 'pointer'});
	
	$("*.hover").hover(function(){
		$(this).addClass("highlight");
	}
	, function(){
		$(this).removeClass("highlight");
	}).css('cursor', 'pointer');
	
	$("#buttonNaviHome")
		.removeClass("ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only")
		.html("&nbsp;")
		.button({
			icons: { primary: "ui-icon-home" },
			text: false
		})
		.click(function(){
			window.location.href = $("#buttonNaviHome").attr('rootPath');
		});
	
	$("#buttonKillSession")
		.removeClass("ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only")
		.html("&nbsp;")
		.button({
			icons: { primary: "ui-icon-trash" },
			text: false
		})
		.click(function(){
			window.location.href = window.location.href + "?clean";
		});
	
	$("#buttonGoMember")
		.button({
			icons: { primary: "ui-icon-person" },
			text: false
		})
		.click(function(){
			$("#dialogsecretform2").dialog("open");
		})
		.show();
	$("#buttonMemberMakeMime")
		.button({
			icons: { primary: "ui-icon-script" },
			text: false
		})
		.click(function(){
			window.location.href = "?a=makeMimeFile";
		})
		.show();
	$("#buttonGoMemberExit")
		.button({
			icons: { primary: "ui-icon-power" },
			text: false
		})
		.click(function(){
			window.location.href = "?logout";
		})
		.show();

	formChecked = false;
	$("#dialogsecretform").dialog({
		autoOpen: false,
		height: 'auto',
		width: 400,
		resizable: false,
		modal: true,
		show : 'fade',
		hide : 'drop',
		buttons: {
			Send: function() {
				if (checkForm("#dialogsecretform")) $("#dialogsecretform .frmDialogsecretform").submit();
				else return false;
			},
			Cancel: function() {
				$(this).dialog("close");
				return false;
			}
		},
		close: function() {
			// leeren
			$("#dialogsecretform #downfilename, #dialogsecretform .validateTips").text("");
			$("#dialogsecretform #secretword").removeClass("ui-state-error");
		}
	});	
	
	$("#dialogsecretform2").dialog({
		autoOpen: false,
		height: 'auto',
		width: 400,
		resizable: false,
		modal: true,
		show : 'fade',
		hide : 'drop',
		buttons: {
			Send: function() {
				if (checkForm("#dialogsecretform2")) $("#dialogsecretform2 .frmDialogsecretform").submit();
				else return false;
			},
			Cancel: function() {
				$(this).dialog("close");
				return false;
			}
		},
		close: function() {
			// leeren
			$("#dialogsecretform2 .validateTips").text("");
			$("#dialogsecretform2 #secretword").removeClass("ui-state-error");
		}
	});
	
	$("#dialogsecretform3").dialog({
		autoOpen: false,
		height: 'auto',
		width: 400,
		resizable: false,
		modal: true,
		show : 'fade',
		hide : 'drop',
		buttons: {
			Send: function() {
				if (checkLockForm()) $("#dialogsecretform3 form:visible").submit();
				else return false;
			},
			Cancel: function() {
				$(this).dialog("close");
				return false;
			}
		},
		close: function() {
			// leeren
			$("#dialogsecretform3 .validateTips").text("");
			$("#dialogsecretform3 #secretword").removeClass("ui-state-error");
		}
	});
});
